<?php
/*******************************************************************************

    FinalsClub.org is a platform for students and faculty to collaborate
    and share their knowledge with the world.

    Copyright (C) 2009  Andrew Magliozzi

    This file is a part of FinalsClub.org.

    FinalsClub.org is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    To contact FinalsClub.org, send an email to info@finalsclub.org, or
    send mail to Finals Club, c/o Veritas Tutors, 1132 Massachusetts Avenue,
    Cambridge, MA 02138.

*******************************************************************************/
/**
 * summaries actions.
 *
 * @package    FinalsClub
 * @subpackage summaries
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class summariesActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $this->forward('default', 'module');
  }
  
  public function executeGroup(sfWebRequest $request)
  {
  	$groupMember = GroupMemberPeer::retrieveByUserIdAndGroupId($this->getUser()->getId(), $request->getParameter('id'));
  	if(!$groupMember)
	{
		$this->forward('group', 'show');
	}
  	
  	//for now only retrieve summaries for books that the group has uploaded
  	$this->group_id = $request->getParameter('id');
  	$this->group = GroupPeer::retrieveById($this->group_id);
	$this->summaries = SummaryPeer::retrieveByGroupId($this->group_id);
	
  }
  
  public function executeShow(sfWebRequest $request)
  {
  	//for now only retrieve summaries for books that the group has uploaded
  	$this->summary = SummaryPeer::retrieveByPK($request->getParameter('id'));
   	$this->forward404Unless(EntitlementPeer::isEntitled($this->summary->getId(),$this->getUser()->getId(), EntitlementPeer::SUMMARY_TYPE ,EntitlementPeer::READ_PERMISSION), "You do not have access to this resource");
  	
  }
  
  
  
  public function executeNew(sfWebRequest $request)
  {
  	//for now only retrieve summaries for books that the group has uploaded
		$group_id = $request->getParameter('group_id');
		$type = $request->getParameter('type');
		$this->form = new SummaryForm(null, array('group_id' => $group_id));
	// 	$oFCKeditor = Utils::getFCKEditor('summary[text]');
		$oFCKeditor = FCKDrawUtil::getFCKDrawEditor('summary[text]');
		$this->oFCKeditor = $oFCKeditor;
		
		$this->isNew = true;
		
		$this->group_id = $group_id;
		$this->setTemplate('edit');
  }
  
  
 public function executeEdit(sfWebRequest $request)
  {
  	//for now only retrieve summaries for books that the group has uploaded
  		$summary = SummaryPeer::retrieveByPK($request->getParameter('id'));
   		$this->forward404Unless(EntitlementPeer::isEntitled($this->summary->getId(),$this->getUser()->getId(), EntitlementPeer::SUMMARY_TYPE , EntitlementPeer::WRITE_PERMISSION), "You do not have access to this resource");
  		
		$group_id = $summary->getGroupId();
		$this->form = new SummaryForm($summary, array('group_id' => $group_id));
	
		// $oFCKeditor = Utils::getFCKEditor('summary[text]');
		$oFCKeditor = FCKDrawUtil::getFCKDrawEditor('summary[text]');
		$oFCKeditor->Value = $summary->getText();
		$this->oFCKeditor = $oFCKeditor;
		$this->summary = $summary;	
		
		$this->isNew = false;
		
		$this->group_id = $group_id;
		$this->setTemplate('edit');
  }
  

	public function executeUpdate(sfWebRequest $request) {
           $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
           $this->forward404Unless($summary = SummaryPeer::retrieveByPk($request->getParameter('id')), sprintf('Those summaries do not exist for id (%s).', $request->getParameter('id')));

			$this->form = new SummaryForm($summary);
			$summaryRequest = $request->getParameter($this->form->getName());
			
			$summaryRequest['user_id'] = $this->getUser()->getId();
			$request->setParameter($this->form->getName(), $summaryRequest);
			$this->summary = $this->processForm($request, $this->form);
			
			$this->setTemplate('show');
		
	}
  
  
  public function executeCreate(sfWebRequest $request)
  {
  		$this->forward404Unless($request->isMethod('post'));
  		
  		//get data
  		$this->form = new SummaryForm(null, array());	
		$summary = $request->getParameter($this->form->getName());
	
		//set user id and process form
  		$summary['user_id'] = $this->getUser()->getId();
		$request->setParameter($this->form->getName(), $summary);	
		$summary = $this->processForm($request, $this->form);

		//redirect page
		if($summary->getGroupId())
		{
			$this->redirect('summaries/group?id='.$summary->getGroupId());
		}
  }
  

	protected function processForm(sfWebRequest $request, sfForm $form) {
		$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
		if ($form->isValid()) {
			$status = $form->getObject()->isNew() ? 'created' : 'updated';
			$this->getUser()->setFlash('success', 'Your Summary has been successfully '.$status);
			$summary = $form->updateObject(); 
			$summary->setUserId($this->getUser()->getId());	// prevent the user from maliciously changing the creator 
			$summary->save();

			return $summary;
			
		}
	}
  
  
}
